package com.huatai.bi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.bi.dto.XxlJobDTO;
import com.huatai.bi.entity.XxlJobGroupEntity;
import com.huatai.bi.entity.XxlJobInfoEntity;
import com.huatai.bi.entity.XxlJobLogglueEntity;
import com.huatai.bi.service.XxlJobGroupService;
import com.huatai.bi.service.XxlJobInfoService;
import com.huatai.bi.service.XxlJobLogglueService;
import com.huatai.bi.vo.XxlJobDictVo;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gusiyi
 * @version 1.0
 * @className XllJobManagerController
 * @description 描述：
 * @date 2024/7/2 16:39
 */
@RestController
@RequestMapping("/xxljob_manager")
@Api(tags = "xxljob管理")
public class XllJobManagerController {

	@Autowired
	private XxlJobInfoService xxlJobInfoService;

	@Autowired
	private XxlJobGroupService xxlJobGroupService;

	@Autowired
	private XxlJobLogglueService xxlJobLogglueService;

	@GetMapping("/jobGroupList")
	@ApiOperation(value = "执行器列表",notes = "执行器列表")
	@ApiOperationSupport(order = 1)
	public R<List<XxlJobGroupEntity>> jobGroupList(){
		return R.data(xxlJobGroupService.list());
	}

	@PostMapping("/pageList")
	@ApiOperation(value = "分页获取定时任务列表", notes = "分页获取定时任务列表")
	@ApiOperationSupport(order = 2,includeParameters = {
		"xxlJobDTO.jobDesc",
		"xxlJobDTO.author",
		"xxlJobDTO.pageIndex",
		"xxlJobDTO.pageSize",
	})
	public R<IPage<XxlJobInfoEntity>> xxlJobInfoPage(@RequestBody XxlJobDTO xxlJobDTO){
		return R.data(xxlJobInfoService.xxlJobInfoPage(xxlJobDTO));
	}

	@PostMapping("/saveOrUpdate")
	@ApiOperation(value = "新增/修改定时任务", notes = "新增/修改定时任务,传id则为修改，不修改则传null")
	@ApiOperationSupport(order = 3,includeParameters = {
		"xxlJobInfoEntity.id",
		"xxlJobInfoEntity.jobGroup",
		"xxlJobInfoEntity.jobDesc",
		"xxlJobInfoEntity.author",
		"xxlJobInfoEntity.alarmEmail",
		"xxlJobInfoEntity.scheduleType",
		"xxlJobInfoEntity.scheduleConf",
		"xxlJobInfoEntity.glueType",
		"xxlJobInfoEntity.executorHandler",
		"xxlJobInfoEntity.executorParam",
		"xxlJobInfoEntity.executorRouteStrategy",
		"xxlJobInfoEntity.childJobid",
		"xxlJobInfoEntity.misfireStrategy",
		"xxlJobInfoEntity.executorBlockStrategy",
		"xxlJobInfoEntity.executorTimeout",
		"xxlJobInfoEntity.executorFailRetryCount",
	})
	public R<Boolean> saveOrUpdate(@RequestBody XxlJobInfoEntity xxlJobInfoEntity){
		if (xxlJobInfoService.saveOrUpdateXxlJob(xxlJobInfoEntity)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@GetMapping("remove")
	@ApiOperation(value = "删除定时任务", notes = "删除定时任务")
	@ApiOperationSupport(order = 4)
	public R<Boolean> remove(Integer id){
		if (xxlJobInfoService.removeJob(id)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@GetMapping("/scheduleTypeList")
	@ApiOperation(value = "获取调度类型列表", notes = "获取调度类型列表")
	@ApiOperationSupport(order = 5)
	public R<List<XxlJobDictVo>> scheduleTypeList(){
		return R.data(xxlJobInfoService.scheduleTypeList());
	}

	@GetMapping("/glueType")
	@ApiOperation(value = "获取执行器类型列表", notes = "获取执行器类型列表")
	@ApiOperationSupport(order = 6)
	public R<List<XxlJobDictVo>> glueType(){
		return R.data(xxlJobInfoService.glueType());
	}

	@GetMapping("/executorRouteStrategyList")
	@ApiOperation(value = "获取路由策略列表", notes = "获取路由策略列表")
	@ApiOperationSupport(order = 7)
	public R<List<XxlJobDictVo>> executorRouteStrategy(){
		return R.data(xxlJobInfoService.executorRouteStrategy());
	}

	@GetMapping("/misfireStrategyList")
	@ApiOperation(value = "调度过期策略列表",notes = "调度过期策略列表")
	@ApiOperationSupport(order = 8)
	public R<List<XxlJobDictVo>> misfireStrategyList(){
		return R.data(xxlJobInfoService.misfireStrategyList());
	}

	@GetMapping("/executorBlockStrategyList")
	@ApiOperation(value = "执行器阻塞策略列表",notes = "执行器阻塞策略列表")
	@ApiOperationSupport(order = 9)
	public R<List<XxlJobDictVo>> executorBlockStrategyList(){
		return R.data(xxlJobInfoService.executorBlockStrategy());
	}

	@GetMapping("/start")
	@ApiOperation(value = "启动定时任务", notes = "启动定时任务")
	@ApiOperationSupport(order = 10)
	public R<Boolean> start(Integer id){
		if (xxlJobInfoService.start(id)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@GetMapping("/stop")
	@ApiOperation(value = "停止定时任务", notes = "停止定时任务")
	@ApiOperationSupport(order = 11)
	public R<Boolean> stop(Integer id){
		if (xxlJobInfoService.stop(id)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@PostMapping("/trigger")
	@ApiOperation(value = "触发定时任务", notes = "触发定时任务")
	@ApiOperationSupport(order = 12,includeParameters = {
		"xxlJobDTO.id",
		"xxlJobDTO.executorParam",
		"xxlJobDTO.addressList",
	})
	public R<Boolean> trigger(@RequestBody XxlJobDTO xxlJobDTO){
		if (xxlJobInfoService.trigger(xxlJobDTO)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@PostMapping("/glueSourceSave")
	@ApiOperation(value = "保存脚本代码", notes = "保存脚本代码")
	@ApiOperationSupport(order = 13,includeParameters = {
		"xxlJobDTO.id",
		"xxlJobDTO.glueSource",
		"xxlJobDTO.glueRemark",
	})
	public R<Boolean> glueSourceSave(@RequestBody XxlJobDTO xxlJobDTO){
		if (xxlJobInfoService.glueIdeSave(xxlJobDTO)) {
			return R.data(null);
		}else {
			return R.fail("");
		}
	}

	@GetMapping("/getGlueSourceList")
	@ApiOperation(value = "获取脚本代码列表", notes = "获取脚本代码列表")
	@ApiOperationSupport(order = 14)
	public R<List<XxlJobLogglueEntity>> getGlueSourceList(@RequestParam("jobId")Integer jobId){
		return R.data(xxlJobInfoService.getGlueSourceList(jobId));
	}

	@GetMapping("/getGlueSource")
	@ApiOperation(value = "获取脚本代码", notes = "获取脚本代码")
	@ApiOperationSupport(order = 15)
	public R<XxlJobLogglueEntity> getGlueSource(@RequestParam("id")Integer id){
		return R.data(xxlJobLogglueService.getById(id));
	}


}
